package com.itheima.controller;


import com.itheima.common.R;
import com.itheima.domain.AddressBook;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {


    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;


    @PutMapping("/default")
    public R<String> setDefaultAddress(@RequestHeader("AuthorizationUser") String token,@RequestBody AddressBook addressBook){
        log.info("设置默认地址 token={} addressBook={}" ,token,addressBook);
        return addressBookService.clientSetDefaultAddress(token, addressBook);
    }

    @GetMapping("/default")
    public R<AddressBook> getDefaultAddress(@RequestHeader("AuthorizationUser") String token){
        log.info("获取默认地址通过token token={} " ,token);
        return addressBookService.clientGetDefaultAddress(token);
    }


    @PostMapping
    public R<AddressBook> addAddress(@RequestHeader("AuthorizationUser") String token,@RequestBody AddressBook addressBook){
        log.info("添加地址通过token={} addressBook={} " ,token,addressBook);
        return addressBookService.clientAddAddress(token, addressBook);
    }

    @GetMapping("list")
    public R<List<AddressBook>> getAddressList(@RequestHeader("AuthorizationUser") String token){
        log.info("展示用户所有地址通过token={} " ,token);
        return addressBookService.clientGetAddressList(token);

    }

    @GetMapping("{id}")
    public R<AddressBook> getAddressById(@PathVariable("id") String id){
        log.info("通过id获取地址 id={} " ,id);
        return addressBookService.clientGetAddressById(id);
    }



    @PutMapping
    public R<String> updateAddress(@RequestBody AddressBook addressBook){
        log.info("修改地址 addressBook={} " ,addressBook);
        return addressBookService.clientUpdateAddress(addressBook);
    }

    @DeleteMapping
    public R<String> deleteAddressById(@RequestParam("ids") String ids){
        log.info("删除地址ids={} " ,ids);
        return addressBookService.clientDeleteAddressById(ids);
    }



}
